﻿Imports System.Data.Linq
Imports System.Linq

Public Class CCountryController
    Dim objDataContext As New TanNamChinhDataContext
    Public Function SelectAllRecord() As IQueryable(Of tblCountry)
        Try
            Return (From c In objDataContext.tblCountries
                          Where c.IsActive = True
                          Order By c.CountryName
                          Select c)
        Catch ex As Exception
            Return Nothing
        End Try
    End Function
    Public Function SelectRecords(ByVal sCountry As String) As IQueryable
        Try
            'Đọc dữ liệu
            Dim Rowss = SelectAllRecord()

            'Xử lý lọc
            If (sCountry <> "") Then
                Rowss = Rowss.Where(Function(p) p.CountryName.Contains(sCountry))
            End If
            Return Rowss

        Catch ex As Exception
            Return Nothing
        End Try
    End Function
    Public Function SelectRecordByID(ByVal _id As Integer) As tblCountry
        Try
            Return (From r In objDataContext.tblCountries
                    Where r.ID_Country = _id And r.IsActive = True
                    Select r).Single
        Catch ex As Exception
            Throw ex
        End Try
    End Function
    Public Function InsertRecord(ByVal LocalTable As tblCountry) As Integer
        Try
            objDataContext.tblCountries.InsertOnSubmit(LocalTable)
            objDataContext.SubmitChanges()
            Return LocalTable.ID_Country
        Catch ex As Exception
            Throw ex
        End Try
    End Function
    Public Sub UpdateRecord(ByVal LocalTable As tblCountry)
        Try
            objDataContext.tblCountries.Attach(LocalTable)
            objDataContext.Refresh(RefreshMode.KeepCurrentValues, LocalTable)
            objDataContext.SubmitChanges(ConflictMode.ContinueOnConflict)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
    Public Sub UpdateRecordWithoutAttach(ByVal LocalTable As tblCountry)
        Try
            objDataContext.Refresh(RefreshMode.KeepCurrentValues, LocalTable)
            objDataContext.SubmitChanges(ConflictMode.ContinueOnConflict)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Public Sub DeleteRecord(ByVal _id As Integer)
        Try
            objDataContext.tblCountries.DeleteOnSubmit( _
              (From c In objDataContext.tblCountries _
               Where c.ID_Country = _id).Single)
            objDataContext.SubmitChanges()
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
    Public Function CheckCountryExists(ByVal sCountryName As String, ByVal id As Integer)
        'cach 1
        Return objDataContext.tblCountries.Any(Function(c) c.CountryName.ToLower = sCountryName.ToLower And c.ID_Country <> id And c.IsActive = True)
    End Function
    Public Sub New()
        objDataContext = New TanNamChinhDataContext
    End Sub
    Public Sub New(ByVal _obj As TanNamChinhDataContext)
        objDataContext = _obj
    End Sub
End Class
